// index.js
// 获取应用实例
const app = getApp()

import user from '../../utils/user.js'
import {
    getInviteCodeMsg,
    joinMakeProject,
    joinPayProject
} from '../../api/app.js'

Page({
    data: {
        focus: true,
        code: '',
    },
    onReady() {},
    focuse() {
        this.setData({
            focus: true
        })
    },
    iptchange(e) {
        this.setData({
            code: e.detail.value
        })
        if (e.detail.value.length === 4) {
            getInviteCodeMsg({
                inviteCode: e.detail.value
            }).then((res) => {
                app.confirm('是否加入项目：' + res.projectName)
                if (res.type === 'pay') {
                    joinPayProject({
                        projectId: res.projectId,
                        userId: user.userId,
                        joinType: 1,
                        createUserId: res.userId
                    }).then(() => {
                        app.toast('加入项目成功！', 'success')
                        setTimeout(() => {
                            wx.switchTab({
                                url: '/pages/index/index'
                            })
                        }, 1600)
                    })
                } else {
                    joinMakeProject({
                        projectId: res.projectId,
                        userId: user.userId,
                    }).then(() => {
                        app.toast('加入项目成功！', 'success')
                        setTimeout(() => {
                            wx.switchTab({
                                url: '/pages/index/index'
                            })
                        }, 1600)
                    })
                }
            })
        }
    },
    async inputNumber(event) {
        console.log(event)
        this.setData({
            code: event.detail.value
        })
        return
        if (event.detail.value.length === 4) {
            this.setData({
                code: ''
            })
            const result = await getInviteCodeMsg({
                inviteCode: event.detail.value
            })
            await app.confirm('是否加入项目：' + result.projectName)
            if (result.type === 'pay') {
                await joinPayProject({
                    projectId: result.projectId,
                    userId: user.userId,
                    joinType: 1,
                    createUserId: result.userId
                })
                app.toast('加入项目成功！', 'success')
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }, 1600)
            } else {
                await joinMakeProject({
                    projectId: result.projectId,
                    userId: user.userId,
                })
                app.toast('加入项目成功！', 'success')
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }, 1600)
            }
        } else {
            this.setData({})
        }
    }
})